<?php

/**
 * boxes actions.
 *
 * @package    sf_sandbox
 * @subpackage boxes
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class boxesActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {

    $this->forward404Unless($request->getParameter('removal'));

    $this->getUser()->setAttribute('removal',$request->getParameter('removal'));
    $this->boxs = Doctrine_Core::getTable('Box')
      ->createQuery('a')
      ->execute();
    $this->freeItems = Doctrine_Core::getTable('Item')
      ->createQuery('a')
      ->execute();
    $this->rooms = Doctrine_Core::getTable('Room')
      ->createQuery('a')
      ->execute();

  }

  public function executeListe(sfWebRequest $request)
  {
    $this->rooms = Doctrine_Core::getTable('Room')
      ->createQuery('a')
      ->execute();
  }

  public function executeAdd2box(sfWebRequest $request)
  {
	$box = Doctrine_Core::getTable('Box')->find(array($request->getParameter('box')));
	$item = Doctrine_Core::getTable('Item')->find(array($request->getParameter('item')));
	$attrib = new BoxAttribution();
	$attrib->setBox($box);
	$attrib->setItem($item);
        $attrib->setRemovalId($this->getUser()->getAttribute('removal'));
	$attrib->save();
  }
  public function executeRem2box(sfWebRequest $request)
  {
        $attrib = Doctrine_Core::getTable('BoxAttribution')->find(array($request->getParameter('box'),$request->getParameter('item')));
        $attrib->delete();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->removal=$request->getParameter('removal');

    $this->box = Doctrine_Core::getTable('Box')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->box);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->removal=$request->getParameter('removal');

    $this->form = new BoxForm();
  }

  public function executeCreate(sfWebRequest $request)
  {

    $this->form = new BoxForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->removal=$request->getParameter('removal');

    $this->forward404Unless($box = Doctrine_Core::getTable('Box')->find(array($request->getParameter('id'))), sprintf('Object box does not exist (%s).', $request->getParameter('id')));
    $this->form = new BoxForm($box);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($box = Doctrine_Core::getTable('Box')->find(array($request->getParameter('id'))), sprintf('Object box does not exist (%s).', $request->getParameter('id')));
    $this->form = new BoxForm($box);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($box = Doctrine_Core::getTable('Box')->find(array($request->getParameter('id'))), sprintf('Object box does not exist (%s).', $request->getParameter('id')));
    $box->delete();

    $this->redirect('boxes/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
	  $form->setRemovalId($this->getUser()->getAttribute('removal'));
      $box = $form->save();

      $this->redirect('boxes/edit?id='.$box->getId());
    }
  }
}
